<?php
/*    This file is part of Nephite Exchange.
 *
 *    Nephite Exchange is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    Nephite Exchange is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Nephite Exchange.  If not, see <http://www.gnu.org/licenses/>.
 */

class Default_Model_NephiteExchangeDataMapper
{
	protected $_dbExchangeRateTable;
	protected $_dbBarleyExchangeTable;
	protected $_dbLastUpdatedTable;
	protected $_dbCommentsTable;

	public function setDbCommentsTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}

		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbCommentsTable = $dbTable;
		return $this;
	}

	public function getDbCommentsTable()
	{
		if (null === $this->_dbCommentsTable) {
			$this->setDbCommentsTable('Default_Model_DbTable_Comments');
		}
		return $this->_dbCommentsTable;
	}

	public function setDbExchangeRateTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}

		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbExchangeRateTable = $dbTable;
		return $this;
	}

	public function getDbExchangeRateTable()
	{
		if (null === $this->_dbExchangeRateTable) {
			$this->setDbExchangeRateTable('Default_Model_DbTable_ExchangeRate');
		}
		return $this->_dbExchangeRateTable;
	}

	public function setDbBarleyExchangeTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}

		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbBarleyExchangeTable = $dbTable;
		return $this;
	}

	public function getDbBarleyExchangeTable()
	{
		if (null === $this->_dbBarleyExchangeTable) {
			$this->setDbBarleyExchangeTable('Default_Model_DbTable_BarleyExchange');
		}
		return $this->_dbBarleyExchangeTable;
	}

	public function setDbLastUpdatedTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}

		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbLastUpdatedTable = $dbTable;
		return $this;
	}

	public function getDbLastUpdatedTable()
	{
		if (null === $this->_dbLastUpdatedTable) {
			$this->setDbLastUpdatedTable('Default_Model_DbTable_LastUpdated');
		}
		return $this->_dbLastUpdatedTable;
	}


	public function setLastUpdated (Default_Model_NephiteExchangeData $neda)
	{
		$result = $this->getDbLastUpdatedTable()->fetchAll();
		if (0 == count ($result)) {
			return;
		}
			
		$row = $result->current();
		$neda->setLastUpdated ($row->LastUpdated);
	}

	public function setBarleyPrice (Default_Model_NephiteExchangeData $neda)
	{
		$result = $this->getDbBarleyExchangeTable()->fetchAll();
		if (0 == count ($result))
		return;
			
		$row = $result->current();
		$neda->setLatestBarleyPrice (sprintf ("%0.2f", $row->CurrentRate));
	}

	public function fetchExchanges (Default_Model_NephiteExchangeData $neda, $search)
	{
		$select = $this->getDbExchangeRateTable()->select();
		if ($search == "RESET") {
			$select->where ("Country IN ('EUROPEAN UNION', 'INDIA', 'CHINA', 'UNITED STATES')");
		} else if ($search !== null) {
			$search = "%$search%";
			$select->where ("Country LIKE ? OR ISOName LIKE ?", $search, $search);
		}

		$rows = $this->getDbExchangeRateTable()->fetchAll($select);

		$results = array ();
		foreach ($rows as $row) {
			$exchangeRate = new Default_Model_ExchangeRate ();
				
			$exchangeRate->setCountry($row->Country)
			->setCurrencyName($row->CurrencyName)
			->setISOName($row->ISOName)
			->setExchange($row->Exchange);

			$results[] = $exchangeRate;
		}

		$neda->setExchangeRates ($results);
	}

	public function getCurrencies(Default_Model_NephiteExchangeData $neda)
	{
		$select = $this->getDbExchangeRateTable()->select();
		$select->distinct();
		$select->from (array ('e' => 'ExchangeRate'), array ('ISOName'));
		$select->order (array('ISOName'));

		$rows = $this->getDbExchangeRateTable()->fetchAll($select);

		$results = array ();
		foreach ($rows as $row) {
			$results[] = $row->ISOName;
		}

		$neda->setCurrencies($results);
	}

	public function addComment ($email, $comment) {
		$data = array(
   		 'commentEmail'      => urldecode($email),
   		 'commentNote' 	     => urldecode($comment)
		);
		
		$this->getDbCommentsTable()->getAdapter()->insert('Comments', $data);
	}

}